#! /usr/bin/env python
#coding=utf-8

"""
    SOCKET测试

"""

import pycurl
c = pycurl.Curl()
c.setopt(pycurl.URL, 'http://mail.qq.com/')
header_list = []
header_list += ["Content-type: application/x-www-form-urlencoded"]
header_list += ["User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .Net CLR 1.1.4322"]
header_list += ["Accept: */*"]
header_list += ["Accept-Language: zh-cn,en-us;q=0.5"]
#header_list += ["Accept-Encoding: gzip,deflate"]

c.setopt(pycurl.HTTPHEADER, header_list)
import StringIO
b = StringIO.StringIO()
c.setopt(pycurl.WRITEFUNCTION, b.write)
c.setopt(pycurl.FOLLOWLOCATION, 1)
c.setopt(pycurl.MAXREDIRS, 5)
c.perform()
#print b.getvalue()

import re
js_url = None
post_url = None
js_PublicKey = None
js_ts = None

text = re.search('<form name="form1" method="post" action="(?P<POST_URL>.+)" onSubmit="return checkInput\(\);"',
    b.getvalue(), re.M | re.S)
if text:
    post_url = text.groupdict().get('POST_URL', None)

text = re.search('<script language="JavaScript" src="(?P<JS_URL>.+)"></script>',
    b.getvalue(), re.M | re.S)
if text:
    js_url = text.groupdict().get('JS_URL', None)

text = re.search(r'var PublicKey = "(?P<POST_PUBLICKEY>[\dA-F]+?)"',
    b.getvalue(), re.M | re.S)
if text:
    js_PublicKey = text.groupdict().get('POST_PUBLICKEY', None)

text = re.search(r'<INPUT type="hidden" name="ts" value="(?P<POST_TS>\d+?)" />',
    b.getvalue(), re.M | re.S)
if text:
    js_ts = text.groupdict().get('POST_TS', None)

import os
js_filename_name = os.path.join(os.path.dirname(__file__), 'temp.js')
js_RSAKey = None

if js_url and js_PublicKey and js_ts:
    c.setopt(pycurl.URL, js_url)
    c.setopt(pycurl.HTTPHEADER, header_list)
    b = StringIO.StringIO()
    c.setopt(pycurl.WRITEFUNCTION, b.write)
    c.setopt(pycurl.FOLLOWLOCATION, 1)
    c.setopt(pycurl.MAXREDIRS, 5)

    c.perform()
    js_str = b.getvalue()
    temp_replace = re.search(r'''if\(j_lm \&\& \(navigator\.appName == "Microsoft Internet Explorer"\)\).+?BigInteger.prototype.DB = dbits;''', 
        js_str, re.M | re.S).group(0)
    js_str = js_str.replace(temp_replace, '''BigInteger.prototype.am = am2;
dbits = 30;
BigInteger.prototype.DB = dbits;''')
    temp_replace = re.search(r'''if\(navigator\.appName == "Netscape".+?}''',
        js_str, re.M | re.S).group(0)
    js_str = js_str.replace(temp_replace, '')    
    file(js_filename_name,'w').write(js_str)
    
    js_exe_name = os.path.join(os.path.dirname(__file__), 'js.exe')
    js_script = r"""var PublicKey = '%s';var RSA = new RSAKey();RSA.setPublic(PublicKey, '10001');var Res = RSA.encrypt('abcd\n%s\n');print(hex2b64(Res));"""%(js_PublicKey, js_ts)
    
    #js_RSAKey = os.popen('%s -f %s -e "%s"'%(js_exe_name, js_filename_name, js_script)).read()
    #os.system('%s -f %s -e "%s"'%(js_exe_name, js_filename_name, js_script))
    js_RSAKey = os.popen('%s -f %s -e "%s"'%(js_exe_name, js_filename_name, js_script)).read()
    print js_RSAKey

